package com.socket;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

//使用Future  得到线程任务返回结果
public class Test {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future<String>> futures = new ArrayList<Future<String>>();
        for (int i = 0; i < 10; i++) {
            //使用future接受处理结果
            Future<String> future = executorService.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("线程名称" + Thread.currentThread().getName());
                    Thread.sleep(3000);
                    return Thread.currentThread().getName();
                }
            });
            futures.add(future);
        }
        try {
            for (Future<String> future : futures) {
                //get方法会阻塞当前线程，直到任务执行完成返回结果
                System.out.println("返回结果=====" + future.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //开始关闭线程池
        executorService.shutdown();
        System.out.println("线程池关闭完成");

    }

    @org.junit.Test
    public void test() {
        OrderService orderService = new OrderService();

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future<Object>> futures = new ArrayList<Future<Object>>();
        for (int i = 0; i < 100; i++) {
            //使用future接受处理结果
            final int id = i;
            Future<Object> future = executorService.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    Object o = orderService.handlerOrder(id);
                    Thread.sleep(1000);
                    return o;
                }
            });
            futures.add(future);
        }
        try {
            for (Future<Object> future : futures) {
                //get方法会阻塞当前线程，直到任务执行完成返回结果
                System.out.println("返回结果=====" + future.get());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int i = 0;

    @org.junit.Test
    public void test3() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        ReentrantLock reentrantLock = new ReentrantLock();
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        for (int i1 = 0; i1 < 1000; i1++) {
            final int j = i1;
            executorService.execute(new Runnable() {
                @Override
                public void run() {

                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        System.out.println(i);

    }

    private static Object A = new Object();
    private static Object B = new Object();

    public static synchronized void incr() {

        Test.i++;

    }

    private void syn() {
        synchronized (A) {
            System.out.println("获得锁Ａ");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (B) {
                System.out.println("获得锁B");
            }
        }
    }

    private void syn1() {
        synchronized (B) {
            System.out.println("获得锁B");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (A) {
                System.out.println("获得锁A");
            }
        }
    }

    @org.junit.Test
    public void test4() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        new Thread(new Task(true)).start();
        new Thread(new Task(false)).start();
        Future submit = executorService.submit(new CallTask());
        System.out.println(submit.get());

    }

    public static class CallTask implements Callable {


        @Override
        public Object call() throws Exception {
            return "哈哈";
        }
    }
    public static class Task implements Runnable {
        public static Object A = new Object();
        public static Object B = new Object();
        private boolean flag;

        public Task(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"运行");
            if (flag) {
                synchronized (A) {
                    System.out.println(Thread.currentThread().getName()+"获得锁A");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (B) {
                        System.out.println(Thread.currentThread().getName()+"获得锁B");
                    }
                }
            } else {
                synchronized (B) {
                    System.out.println(Thread.currentThread().getName()+"获得锁B");

                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (A) {
                        System.out.println(Thread.currentThread().getName()+"获得锁A");
                    }
                }
            }

        }
    }
}